<?php
namespace App\Api\Controllers\AlipayOpen;


use Alipayopen\Sdk\AopClient;
use Alipayopen\Sdk\Request\AlipayOpenIotDeviceBindRequest;
use Alipayopen\Sdk\Request\AntMerchantExpandOrderQueryRequest;
use App\Api\Controllers\Config\AlipayIsvConfigController;
use App\Models\AlipayHbOrder;
use App\Models\AlipayHbrate;
use App\Models\AlipayUserHbrate;
use App\Models\AlipayZftStore;
use App\Models\Store;
use App\Models\User;
use Illuminate\Http\Request;

class IotController extends \App\Api\Controllers\BaseController
{
    //
    public function iot_device_bind(Request $request)
    {
        try {
            $user_token = $this->parseToken();

            $store_id = $request->get('store_id', '');
            $merchant_id_type = $request->get('merchant_id_type', 'direct');
            $merchant_alipid = $request->get('merchant_alipid', '');
            $device_id = $request->get('device_id', '');
            $mini_app_id = $request->get('mini_app_id', '');
            $config_id = $request->get('config_id', $user_token->config_id);
            $supplier_id = $request->get('supplier_id', '');
            $biz_tid = $request->get('biz_tid', '');

            $config_type = '01';
            //配置
            $isvconfig = new AlipayIsvConfigController();
            $config = $isvconfig->AlipayIsvConfig($config_id, $config_type);

            $aop = new AopClient();
            $aop->apiVersion = "2.0";
            $aop->appId = $config->app_id;
            $aop->rsaPrivateKey = $config->rsa_private_key;
            $aop->alipayrsaPublicKey = $config->alipay_rsa_public_key;
            $aop->method = 'alipay.open.iot.device.bind';
            $aop->signType = "RSA2";//升级算法
            $aop->gatewayUrl = $config->alipay_gateway;
            $aop->format = "json";
            $aop->charset = "GBK";
            $aop->version = "2.0";
            $requests = new AlipayOpenIotDeviceBindRequest();

            $data_re_arr = array(
                'mini_app_id' => $mini_app_id,
                'device_id_type' => 'ID',
                'biz_tid' => $biz_tid,
                'supplier_id' => $supplier_id,
                'device_sn' => $device_id,
                'external_id' => $store_id,
                'source' => $config->alipay_pid,
                'merchant_id_type' => $merchant_id_type,
                'merchant_id' => $merchant_alipid,
            );

            $data_re = json_encode($data_re_arr);

            $requests->setBizContent($data_re);
            $result = $aop->execute($requests, null, '');

            $responseNode = str_replace(".", "_", $requests->getApiMethodName()) . "_response";
            $resultCode = $result->$responseNode->code;
            //异常
            if ($resultCode == 40004) {
                return json_encode([
                    'status' => 2,
                    'message' => $result->$responseNode->sub_msg . $result->$responseNode->sub_code,
                    'result_code' => $resultCode,
                ]);
            } else {

                if (!empty($resultCode) && $resultCode == 10000) {
                    return json_encode([
                        'status' => 1,
                        'message' => '绑定成功',
                        'data' => $data_re_arr,
                    ]);
                } else {
                    return json_encode([
                        'status' => 2,
                        'message' => $result->$responseNode->sub_msg . $result->$responseNode->sub_code,
                        'result_code' => $resultCode,
                    ]);
                }

            }

        } catch (\Exception $exception) {
            return json_encode([
                'status' => 2,
                'message' => $exception->getMessage(),
            ]);
        }
    }


}
